home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / PredatorPrey / calc_files.c < prev    next >
C/C++ Source or Header  |  1996-06-22  |  22KB  |  898 lines

  1. /*    look at this    */
  2.  
  3.             /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
  4.             /*                                                               */
  5.             /*                    Prototype HP15C Calculator                */
  6.             /*                    James C. Ullrey                            */
  7.             /*                    INRESCO                                    */
  8.             /*                    © 1990                                    */
  9.             /*                    Version    13.97a                            */
  10.             /*                                                               */
  11.             /*                    FILE SEGMENT                               */
  12.             /*                                                               */
  13.             /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
  14.  
  15. #define jcuNewOrigin    FALSE    /*TRUE*/
  16.  
  17. /*****************************************************************/
  18. /*  I N C L U D E S                                                 */
  19. /*****************************************************************/
  20.  
  21. #ifndef __C14__
  22. #include    "PredatorPrey.h"
  23. #endif
  24. #include "Globals.h"
  25.  
  26. #include <Lists.h>
  27. #include <Resources.h>
  28. #include "PredatorPreyData.h"
  29. #include "calc_files.h"
  30. #include "ResourceDefs.h"
  31. #include  "calc_dipshit.h"
  32. #include "Miscellany.h"
  33.  
  34.  
  35. /********************************************************************
  36. /*  G L O B A L   V A R I A B L E   D E C L A R A T I O N S
  37. /********************************************************************/
  38. extern    Boolean        newCreated;
  39. extern    Boolean        preRegistered;
  40. extern    WindowPtr    gMy_windows[];
  41. extern    long        gState;
  42. extern    long        gsState;
  43. extern    long         sto;
  44. extern    long         rcl;
  45. /*extern    long     gNewNum;*/
  46. /*extern    long     canClear;*/
  47. /*extern    long     gRoll;*/
  48. extern    long         wait;
  49. extern    long         gFlag;
  50. extern    long         fFlag;
  51. extern    long         sWait;
  52. extern    long         rWait;
  53. extern    long         lblWait;
  54. extern    long         plus;
  55. extern    long         minus;
  56. extern    long         timesF;
  57. extern    long         divide;
  58. extern    long         gDot;
  59. /*extern    long     rsFlag;*/
  60. extern    long    gDigits;
  61. /*extern    long    runMode;*/
  62. /*extern    long    gsbWait;*/
  63. extern    long        gtoWait;
  64. //extern    short        sRefNum;            /*    Log file 2    */
  65. //extern    short        rRefNum;            /*    Log file 1    */
  66. extern    short        qRefNum;            /*    map data    */
  67. extern    short        pRefNum;
  68. extern    short        SRcount;
  69. extern    Boolean        hasPlot;
  70. extern    char        tempString[256];
  71.  
  72. /*****************************************************************/
  73. /*  P R O T O T Y P E S
  74. /*****************************************************************/
  75.     ListHandle    CreateList            (    WindowPtr    theWindow,
  76.                                         short        rowInt,
  77.                                         short        colInt);
  78.  
  79.         void    IndexDeparse        (    short        i);
  80.         short    do_save_file         (WindowPtr wPtr, short do_sfput);
  81.  
  82.         void     close_the_window    (WindowPtr wPtr);
  83.                                         
  84.         OSErr    get_c_filespec        (short wd_refnum, long *dir_id, short *vol_refnum);
  85.  
  86.         void    CreateDefaultPreference(void);
  87.         
  88.         void    CreateNewFile        (    OSType inCreator,
  89.                                         OSType inFileType,
  90.                                         ScriptCode inScriptCode /*= smSystemScript*/);
  91.                                         
  92.         void    SavePreferences        (void);                                
  93.         short    OpenDataFork        (short    inPrivileges);
  94.         long    WriteDataFork        (    const void    *inBuffer,
  95.                                         long        inByteCount);
  96.         
  97. /*****************************************************************/
  98. /*****************************************************************/
  99. /*
  100. /* R O U T I N E S
  101. /*
  102. /*****************************************************************/
  103. /*****************************************************************/
  104.  
  105.  
  106. void    file_seg() {}        /*  for reference in "UnloadSeg()" calls    */
  107.  
  108.  
  109. /********************** SetUpFile ************************/
  110. /********************** SetUpFile ************************/
  111. /********************** SetUpFile ************************/
  112.  
  113. void SetUpFile( Str255    *fn,  short        *refNum )    /* called from main()    */
  114. {
  115.     short            io;
  116.     short            i;
  117.     short            notDone = 1;
  118.     long            count,fcount;
  119.     Str255            sresult;
  120.     SFReply         reply;
  121.     OSErr             error,fsErr,iErr;
  122.     OSType            folderType;
  123.     FSSpec            *spec, theSpec;
  124.     Boolean            tryToFindPrefs = TRUE;
  125.     Boolean            createFolder = TRUE;
  126.     short            foundVRefNum;
  127.     long            foundDirID;
  128.  
  129.     /*newCreated = FALSE;*/
  130.     //spec = &theSpec;
  131.     //fsErr = FindFolder(kOnSystemDisk, 'pref',kCreateFolder, &(spec->vRefNum), &(spec->parID) );
  132.     //fsErr = FSMakeFSSpec(spec->vRefNum, spec->parID, "\pDefault Prefs", spec);
  133.     //if(fsErr == fnfErr)
  134.     //{
  135.     //    CreateDefaultPreference();
  136.     //}
  137.     
  138.     count = sizeof(map);
  139.     while(notDone)
  140.     {
  141.         //if (tryToFindPrefs)
  142.         //{
  143.         //    folderType = 'pref';
  144.         //    iErr = FindFolder(refNum, folderType, createFolder, &foundVRefNum, &foundDirID);
  145.         //}
  146.         //else
  147.         //{
  148.         error = FSOpen(*fn, *refNum,&qRefNum);                /* II-91, IV-109    */
  149.         if(error == fnfErr || error == bdNamErr)
  150.         {    
  151.             error = Create( *fn, *refNum, 'INRS', 'INRC');    /* II-90, IV-112    */
  152.             error = FSOpen(*fn, *refNum,&qRefNum);
  153.             /*newCreated = TRUE;*/
  154.             InitMapData(fn,*refNum,qRefNum);        /* in C14CalculatorData.c */
  155.         }
  156.         else
  157.         {
  158.             error = SetFPos(qRefNum,1,0);                    /* II-93, IV-110    */
  159.             error = FSRead(qRefNum,&count,&map);            /* II-92, IV-109    */
  160.             if(map.regstat == 0 && map.memstat == 0)
  161.             {
  162.                 map.memstat = 19;
  163.                 map.regstat = 65 - map.memstat - map.memcom;
  164.             }
  165.         }
  166.         //}
  167.         /*error = FSClose(qRefNum);*/                        /* II-94, IV-112    */
  168.         if(!error)
  169.             notDone = 0;
  170.     }
  171.     notDone = 1;
  172. }
  173. /*********************************************************/
  174. /******************** SetUpLogFile ***********************/
  175. /*********************************************************/
  176. /************    called from calc_mousedown.c    **********/
  177. /*********************************************************/
  178.  
  179. OSErr        SetUpLogFile    (short selector,Str255 *fn,short *refNum,short    *fRefNum)
  180. {
  181.     //static            char    logState;
  182.     short            io;
  183.     short            i;
  184.     short            notDone = 1;
  185.     short            theRefNum;
  186.     long            count,fcount;
  187.     Str255            sresult;
  188.     SFReply         reply;
  189.     OSErr             error;
  190.     OSErr             err;
  191.     Boolean            okay;
  192.     char            *message;
  193.     
  194.     
  195.     
  196.     //if(selector ==     0)
  197.     //{
  198.         //theRefNum = *rRefNum;
  199.     //}
  200.     //else if (selector == 1)
  201.     //{
  202.         //theRefNum = sRefNum;
  203.     //}
  204.         
  205.     okay = CheckOS    (error = FSOpen(*fn, *refNum, &theRefNum));
  206.                 /* II-91, IV-109    */
  207.     if(error == fnfErr || error == bdNamErr)
  208.     {    
  209.         error = Create( *fn, *refNum, 'INRS', 'TEXT');    /* II-90, IV-112    */
  210.         error = FSOpen(*fn, *refNum,&theRefNum);
  211.         if(!error)        ;
  212.         else
  213.         {
  214.             DebugStr("\pHello from FSOpen error, fnfErr or bdNamErr");
  215.         }
  216.         error = SetFPos(theRefNum,1,0);
  217.         if(!error)        ;
  218.         else
  219.         {
  220.             switch(error)
  221.             {
  222.                 case -39:
  223.                     DebugStr("\pHello from SetFPos error, Attempt to position past the end-of-file");
  224.                     break;
  225.                 case -58:
  226.                     DebugStr("\pHello from SetFPos error, External file system");
  227.                     break;
  228.                 case -38:
  229.                     DebugStr("\pHello from SetFPos error, File not open");
  230.                     break;
  231.                 case -36:
  232.                     DebugStr("\pHello from SetFPos error, I/O error");
  233.                     break;
  234.                 case -40:
  235.                     DebugStr("\pHello from SetFPos error, Can't position to before start of file");
  236.                     break;
  237.                 case -51:
  238.                     DebugStr("\pHello from SetFPos error, Bad fRefNum");
  239.                     break;
  240.             }
  241.         }
  242.             
  243. #ifdef    jcuNewOrigin
  244.  
  245. /*        if(selector ==     2)*/
  246. /*        {*/
  247. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  248. /*            DoLogNum(theRefNum,10);            /*    .    »*/
  249. /*            DoLogNum(theRefNum,2);            /*    2    »*/
  250. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  251. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  252. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  253. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  254. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  255. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  256. /*            DoLogInit(theRefNum,7);            /*    CR    »*/
  257. /*    */
  258. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  259. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  260. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  261. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  262. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  263. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  264. /*            DoLogNum(theRefNum,1);            /*    1    »*/
  265. /*            DoLogInit(theRefNum,7);            /*    CR    »*/
  266. /*            */
  267. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  268. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  269. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  270. /*            DoLogNum(theRefNum,10);            /*    .    »*/
  271. /*            DoLogNum(theRefNum,2);            /*    2    »*/
  272. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  273. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  274. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  275. /*            DoLogNum(theRefNum,2);            /*    2    »*/
  276. /*            DoLogInit(theRefNum,7);            /*    CR    »*/
  277. /*    */
  278. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  279. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  280. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  281. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  282. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  283. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  284. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  285. /*            DoLogInit(theRefNum,7);            /*    CR    »*/
  286. /*    */
  287. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  288. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  289. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  290. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  291. /*            DoLogNum(theRefNum,0);            /*    0    »*/
  292. /*            DoLogNum(theRefNum,10);            /*    .    »*/
  293. /*            DoLogNum(theRefNum,2);            /*    2    »*/
  294. /*            DoLogInit(theRefNum,5);            /*    Tab    »*/
  295. /*            DoLogNum(theRefNum,3);            /*    3    »*/
  296. /*            DoLogInit(theRefNum,7);            /*    CR    »*/
  297. /*        }*/
  298. #else            
  299.         DoLogNum(theRefNum,0);            /*    0    */
  300.         DoLogNum(theRefNum,10);            /*    .    */
  301.         DoLogNum(theRefNum,0);            /*    0    */
  302.         DoLogNum(theRefNum,0);            /*    0    */
  303.         DoLogNum(theRefNum,0);            /*    0    */
  304.         DoLogNum(theRefNum,0);            /*    0    */
  305.         DoLogNum(theRefNum,9);            /*    9    */
  306.         DoLogInit(theRefNum,5);            /*    Tab    */
  307.         DoLogNum(theRefNum,0);            /*    0    */
  308.         DoLogInit(theRefNum,5);            /*    Tab    */
  309.         DoLogNum(theRefNum,0);            /*    0    */
  310.         DoLogInit(theRefNum,5);            /*    Tab    */
  311.         DoLogNum(theRefNum,0);            /*    0    */
  312.         DoLogInit(theRefNum,7);            /*    CR    */
  313.         
  314.         DoLogNum(theRefNum,0);            /*    0    */
  315.         DoLogInit(theRefNum,5);            /*    Tab    */
  316.         DoLogNum(theRefNum,0);            /*    0    */
  317.         DoLogInit(theRefNum,5);            /*    Tab    */
  318.         DoLogNum(theRefNum,0);            /*    0    */
  319.         DoLogInit(theRefNum,5);            /*    Tab    */
  320.         DoLogNum(theRefNum,1);            /*    1    */
  321.         DoLogInit(theRefNum,7);            /*    CR    */
  322.         
  323.         DoLogNum(theRefNum,0);            /*    0    */
  324.         DoLogInit(theRefNum,5);            /*    Tab    */
  325.         DoLogNum(theRefNum,0);            /*    0    */
  326.         DoLogNum(theRefNum,1);            /*    1    */
  327.         DoLogNum(theRefNum,5);            /*    5    */
  328.         DoLogNum(theRefNum,0);            /*    0    */
  329.         DoLogNum(theRefNum,10);            /*    .    */
  330.         DoLogNum(theRefNum,0);            /*    0    */
  331.         DoLogInit(theRefNum,5);            /*    Tab    */
  332.         DoLogNum(theRefNum,0);            /*    0    */
  333.         DoLogInit(theRefNum,5);            /*    Tab    */
  334.         DoLogNum(theRefNum,2);            /*    2    */
  335.         DoLogInit(theRefNum,7);            /*    CR    */
  336.  
  337.         DoLogNum(theRefNum,0);            /*    0    */
  338.         DoLogInit(theRefNum,5);            /*    Tab    */
  339.         DoLogNum(theRefNum,0);            /*    0    */
  340.         DoLogInit(theRefNum,5);            /*    Tab    */
  341.         DoLogNum(theRefNum,0);            /*    0    */
  342.         DoLogInit(theRefNum,5);            /*    Tab    */
  343.         DoLogNum(theRefNum,0);            /*    0    */
  344.         DoLogInit(theRefNum,7);            /*    CR    */
  345.  
  346.         DoLogNum(theRefNum,0);            /*    0    */
  347.         DoLogInit(theRefNum,5);            /*    Tab    */
  348.         DoLogNum(theRefNum,0);            /*    0    */
  349.         DoLogInit(theRefNum,5);            /*    Tab    */
  350.         DoLogNum(theRefNum,0);            /*    0    */
  351.         DoLogNum(theRefNum,10);            /*    .    */
  352.         DoLogNum(theRefNum,0);            /*    0    */
  353.         DoLogNum(theRefNum,0);            /*    0    */
  354.         DoLogNum(theRefNum,0);            /*    0    */
  355.         DoLogNum(theRefNum,0);            /*    0    */
  356.         DoLogNum(theRefNum,0);            /*    0    */
  357.         DoLogNum(theRefNum,0);            /*    0    */
  358.         DoLogNum(theRefNum,0);            /*    0    */
  359.         DoLogNum(theRefNum,0);            /*    0    */
  360.         DoLogNum(theRefNum,2);            /*    2    */
  361.         DoLogInit(theRefNum,5);            /*    Tab    */
  362.         DoLogNum(theRefNum,3);            /*    3    */
  363.         DoLogInit(theRefNum,7);            /*    CR    */
  364.         
  365. #endif            
  366.             //logState = '1';
  367.             
  368.             /*    1 is Red    */
  369.             /*    2 is Green    */
  370.             /*    3 is Blue    */
  371.             /*    4 is White    */
  372.             /*    5 is Tab    */
  373.             /*    6 is Space    */
  374.             /*    7 is CR    */
  375.             /*newCreated = TRUE;*/
  376.             //InitMapData(fn,*refNum,rRefNum);        /* in C14CalculatorData.c */
  377.     }
  378.  
  379.     if(error == opWrErr)
  380.     {
  381.         
  382.         err = FSClose(theRefNum);                        /* II-94, IV-112    */
  383.         if(!err);
  384.         else
  385.         {
  386.             ;
  387.         }
  388.         //DebugStr("\pHello from FSOpen error, file is already open with write permission");
  389.     }
  390.     else if(okay)
  391.     {
  392.         //error = SetFPos(theRefNum,1,0);                    /* II-93, IV-110    */
  393.         SetFPos(theRefNum,2,0);                    /* II-93, IV-110    */    /*    2 means from end    */
  394.     }
  395.     *fRefNum = theRefNum;
  396. /*    if(selector ==     0)*/
  397. /*    {*/
  398. /*        rRefNum = theRefNum;*/
  399. /*    }*/
  400. /*    else if (selector ==     1)*/
  401. /*    {*/
  402. /*        sRefNum = theRefNum;*/
  403. /*    }*/
  404.     return(error);
  405. }
  406. /*********************************************************/
  407. /**************** End of SetUpLogFile ********************/
  408. /*********************************************************/
  409.  
  410. /*void    CreateDefaultPreference(void)*/
  411. /*{*/
  412. /*    OSType         mCreator = 'INRS';*/
  413. /*    Preference    mPrefs;*/
  414. /*    */
  415. /*    Handle prefs;*/
  416. /*    */
  417. /*    // If the preference file does not exist, then load the*/
  418. /*    //  'pref' resource in the application's resource fork.*/
  419. /*    //  Use that as the default data for a new preference file.*/
  420. /*    /*prefFile->CreateNewFile(mCreator, 'pref', -1);»        //    original*/
  421. /*    */
  422. /*    mMacFileSpec.vRefNum = 0;    // ??? Is this a good default for*/
  423. /*    mMacFileSpec.parID = 0;        //  a File Spec ???*/
  424. /*    mMacFileSpec.name[0] = 0;*/
  425. /**/
  426. /*    */
  427. /*    CreateNewFile(mCreator, 'pref', -1);                    //    new*/
  428. /*    prefs = GetResource('pref', 128);*/
  429. /*    mPrefs = **(Preference**)prefs;*/
  430. /*    ReleaseResource(prefs);*/
  431. /*    SavePreferences();*/
  432. /*}*/
  433.  
  434. /*void    CreateNewFile(*/
  435. /*    OSType        inCreator,*/
  436. /*    OSType        inFileType,*/
  437. /*    ScriptCode    inScriptCode)*/
  438. /*{*/
  439. /*    FSpCreateResFile(&mMacFileSpec, inCreator, inFileType, inScriptCode);*/
  440. /*    //ThrowIfResError_();*/
  441. /*}*/
  442.  
  443.  
  444. //----------------------------------
  445. // SavePreferences
  446. //    Write data to file
  447. //----------------------------------
  448. /*void SavePreferences()*/
  449. /*{*/
  450. /*    OpenDataFork(fsCurPerm);*/
  451. /*    WriteDataFork(&mPrefs, sizeof(Preference));*/
  452. /*    CloseDataFork();*/
  453. /*}*/
  454.  
  455.  
  456. /*short    OpenDataFork(short    inPrivileges)*/
  457. /*{*/
  458. /*    OSErr    err;*/
  459. /*    */
  460. /*    err = FSpOpenDF(&mMacFileSpec, inPrivileges, &mDataForkRefNum);*/
  461. /*    if(err != noErr)*/
  462. /*    {*/
  463. /*        mDataForkRefNum = refNum_Undefined;*/
  464. /*    }*/
  465. /*}*/
  466.  
  467.  
  468. /*long    WriteDataFork(*/
  469. /*    const void    *inBuffer,*/
  470. /*    long        inByteCount)*/
  471. /*{*/
  472. /*    long    bytesWritten = inByteCount;*/
  473. /*    OSErr    err;*/
  474. /*    */
  475. /*    err = SetFPos(mDataForkRefNum, fsFromStart, 0);*/
  476. /*}*/
  477.  
  478. void    save_window(WindowPtr wPtr)
  479. {
  480.     short        user_response = 0;
  481.     char        w_title[255];
  482.     WObjsHandle    w_objs_hndl;
  483.     short        the_object;
  484.  
  485.  
  486. /***
  487.     First, check if the window pointer is a valid one for the application.
  488.     If so, check to see if the window has been written to but not saved.
  489. ***/        
  490.  
  491.  
  492.     if(!one_of_mine(wPtr)) return;
  493.  
  494.     w_objs_hndl = (WObjsHandle)GetWRefCon(wPtr);    
  495.     if(w_objs_hndl == NIL) return;
  496.     
  497.         /*w_objs_hndl=(WObjs**)GetWRefCon(wPtr);*/
  498.     the_object = (**w_objs_hndl).paletteSetting;
  499.  
  500.  
  501.     if(the_object == PLOT_OBJ)
  502.     {
  503.         hasPlot = FALSE;    /*    reset hasPlot    */
  504.         /*DeleteMenu( PLOT_MENU_ID );*/
  505.         DeleteMenu( PlotID );
  506.         DrawMenuBar();
  507.     }
  508.  
  509.  
  510.  
  511.     if((**w_objs_hndl).dirty)
  512.     {
  513.         GetWTitle(wPtr, (StringPtr)w_title);
  514.         ParamText((StringPtr)w_title, "\p", "\p", "\p");
  515.             
  516.         #ifdef      powerc
  517.             user_response = CautionAlert(SAVE_CHANGES, (UniversalProcPtr)NIL);
  518.         #else
  519.             user_response = CautionAlert(SAVE_CHANGES, NIL);
  520.         #endif      /* powerc */
  521.                         
  522.         switch(user_response)
  523.         {
  524.             case SAVE_YES:
  525.                 #ifdef      powerc
  526.                     StopAlert (UNDER_CONSTRUCTION, (UniversalProcPtr)NIL);
  527.                 #else
  528.                     StopAlert (UNDER_CONSTRUCTION,    NIL);
  529.                 #endif      /* powerc */    
  530.                 if(do_save_file(wPtr, DO_SAVE) == SUCCESS)
  531.                         close_the_window(wPtr);
  532.                 else
  533.                         gDone = FALSE;
  534.                 break;
  535.                 
  536.             case SAVE_NO:
  537.                 close_the_window(wPtr);
  538.                 break;
  539.                 
  540.             case SAVE_CANCEL:
  541.                 gDone = false;       /* if "Cancel", stop any "quit" operation */
  542.                 break;
  543.                 
  544.         }  /*  end of switch    */
  545.         
  546.     }  /*  end of if(**w_objs_hndl).dirty)    */
  547.     
  548.     
  549.     else close_the_window(wPtr);
  550.                     
  551.                                     
  552. }  /*  end of "save_window()"    */
  553.  
  554.  
  555. void    close_the_window(WindowPtr wPtr)
  556. {
  557.     short        n;
  558.     WObjsHandle    w_objs_hndl;
  559.  
  560.     
  561.  
  562. /*************    If it isn't one of our windows, skip it!    ******************/
  563.  
  564.  
  565.     if( !(n = one_of_mine(wPtr)) ) return;
  566.  
  567.     
  568.  
  569. /*****    Else dispose the controls and window, and clear our list    **********/
  570.  
  571.  
  572.     w_objs_hndl = (WObjsHandle)GetWRefCon(wPtr);
  573.     if(w_objs_hndl != NIL)
  574.     {
  575.         HLock((Handle)w_objs_hndl);
  576.         /*    look at this    */
  577.         if((**w_objs_hndl).prInfoHndl)    DisposHandle((Handle)(**w_objs_hndl).prInfoHndl);
  578.         DisposHandle((Handle)w_objs_hndl);
  579.     }
  580.     
  581.     DisposeWindow(gMy_windows[n]);    /*  also does a "KillControls()"    */
  582.     
  583.     gMy_windows[n] = NIL;
  584.  
  585.  
  586. }  /*  end of close_the_window()    */
  587.  
  588.  
  589. short    do_save_file(WindowPtr wPtr,short do_what)
  590. //WindowPtr wPtr;
  591. //short    do_what;
  592.  
  593. {
  594.     short        file_ref_num, wd_refnum, vol_refnum;
  595.     Point        where;
  596.     long        count, dir_id;
  597.     OSErr        error_code;
  598.     OSType        the_creator, our_type;
  599.     char        local_str[256], window_name[256];
  600.     char        *srce_ptr, *dest_ptr;
  601.     char        **dlog_str_hndl;
  602.     Ptr            data_ptr;
  603.     WObjsHandle    w_objs_hndl;
  604.     THPrint        pr_info_hndl;
  605.  
  606.     
  607.  
  608. /*********    If our window data structure is absent, don't even start this!    **********/
  609.  
  610.  
  611.     w_objs_hndl = (WObjsHandle)GetWRefCon(wPtr);
  612.     
  613.     if(w_objs_hndl == NIL) return(FILE_ERROR);
  614.  
  615.     
  616.  
  617. /*************    Set up some initial values and lock our window data         **********/
  618.  
  619.  
  620.     count = 0;
  621.     the_creator = CREATOR_CODE;        /*  our "creator type"                    */
  622.     our_type    = TYPE_CODE;        /*  our "file type"                        */
  623.     SetPt(&where, 100, 80);            /*  where to put the file dialog boxes    */
  624.     
  625.     MoveHHi((Handle)w_objs_hndl);
  626.     HLock ((Handle)w_objs_hndl);
  627.  
  628.  
  629. /**
  630.     Note:
  631.     
  632.     The "findFile" element of the window data structure is of type SFReply.
  633.     The structure of the SFReply record is as follows:
  634.             
  635.             typedef struct
  636.             {
  637.                 Boolean        good;
  638.                 Boolean        copy;
  639.                 fType        OSType;
  640.                 short        vrefNum;
  641.                 short        version;
  642.                 char        fName[255];
  643.             }SFReply;
  644. **/
  645.     
  646.  
  647. /*********    Build up the dialog prompt: "Save (filename) as:"    **********/
  648.  
  649. /**    
  650.     Use strings save in 'STR " resources named "Save" and "As", 
  651.     and the file name from the "findFile" item in the window info record.
  652. **/
  653.  
  654.     
  655.     local_str[0] = 0;
  656.     dest_ptr = local_str;
  657.     
  658.     dlog_str_hndl = (char**)GetString(SAVE_ID);
  659.     MoveHHi(dlog_str_hndl);
  660.     HLock  (dlog_str_hndl);
  661.     
  662.         srce_ptr = (char*)*dlog_str_hndl;
  663.         cat_strings(srce_ptr, dest_ptr, 255);
  664.         
  665.     HUnlock(dlog_str_hndl);
  666.  
  667.     GetWTitle(wPtr, (StringPtr)window_name);
  668.     
  669.     srce_ptr = window_name;        
  670.     cat_strings(srce_ptr, dest_ptr, 255);
  671.  
  672.     
  673.     dlog_str_hndl = (char**)GetString(AS_ID);
  674.     MoveHHi(dlog_str_hndl);
  675.     HLock  (dlog_str_hndl);
  676.     
  677.         srce_ptr = (char*)*dlog_str_hndl;
  678.         cat_strings(srce_ptr, dest_ptr, 255);
  679.         
  680.     HUnlock(dlog_str_hndl);
  681.  
  682.     
  683.  
  684. /***
  685.     If we need to specify a file name, do SFPut thing...
  686. ***/
  687.  
  688.  
  689.     if( (do_what == DO_SFPUT) ||
  690.         ((**w_objs_hndl).hasRealName == FALSE) )
  691.     {
  692.         InitCursor();
  693.         #ifdef powerc 
  694.             SFPutFile(    where,
  695.                         (StringPtr)local_str,
  696.                         (StringPtr)window_name,
  697.                         (UniversalProcPtr)NIL,
  698.                         &(**w_objs_hndl).findFile);
  699.         #else
  700.             SFPutFile(    where,
  701.                         (StringPtr)local_str,
  702.                         (StringPtr)window_name,
  703.                         NIL,
  704.                         &(**w_objs_hndl).findFile);
  705.         #endif
  706. /***
  707.     If the user cancelled the operation, restore the window name
  708. ***/
  709.         
  710.         if ((**w_objs_hndl).findFile.good == FALSE)
  711.         {
  712.             dest_ptr = (char*)((**w_objs_hndl).findFile.fName);
  713.             (**w_objs_hndl).findFile.fName[0] = 0;
  714.             cat_strings(window_name, dest_ptr, 255);
  715.             
  716.             HUnlock((Handle)w_objs_hndl);
  717.             return(CANCELED);
  718.         }
  719.         
  720.  
  721. /***
  722.     Otherwise, get the vol_refnum and create the file
  723. ***/
  724.  
  725.         else
  726.         {
  727.             wd_refnum = (**w_objs_hndl).findFile.vRefNum;
  728.     
  729.             error_code = get_c_filespec(wd_refnum, &dir_id, &vol_refnum);
  730.             if(error_code != noErr) return(FILE_ERROR);
  731.  
  732.             error_code = Create(
  733.                             (**w_objs_hndl).findFile.fName,
  734.                             wd_refnum,
  735.                             the_creator,
  736.                             our_type);
  737.                             
  738.             if( (error_code != noErr) && 
  739.                 (error_code != dupFNErr) )
  740.             {
  741.                 #ifdef      powerc
  742.                     StopAlert(CREATE_ERR, (UniversalProcPtr)NIL);
  743.                 #else
  744.                     StopAlert(CREATE_ERR,    NIL);
  745.                 #endif      /* powerc */                
  746.                 HUnlock((Handle)w_objs_hndl);
  747.                 return(CREATE_ERR);
  748.             }
  749.             else  (**w_objs_hndl).hasRealName = TRUE;
  750.         }
  751.     }
  752.  
  753.     
  754.  
  755. /*************    Save the data in the specified file                            **********/
  756.  
  757.  
  758. /***    a)  open the file    ***/
  759.  
  760.     if( (do_what == DO_SFPUT) ||
  761.         (do_what == DO_SAVE) ) 
  762.     {
  763.         wd_refnum = (**w_objs_hndl).findFile.vRefNum;
  764.     
  765.         error_code = get_c_filespec(wd_refnum, &dir_id, &vol_refnum);
  766.         if(error_code != noErr) return(FILE_ERROR);
  767.  
  768.         error_code = FSOpen(
  769.                         (**w_objs_hndl).findFile.fName,
  770.                         wd_refnum,
  771.                         &file_ref_num);
  772.     
  773.         if    ((error_code != noErr) || (file_ref_num == 0) )
  774.         {
  775.             StopAlert(OPEN_ERR,0);
  776.             HUnlock((Handle)w_objs_hndl);
  777.             return(CANCELED);
  778.         }
  779.     
  780.  
  781.  
  782. /***    b)  check to see that there's disc space    ***/
  783.  
  784.         count = sizeof(fileDataRecord);
  785.         error_code = SetEOF(file_ref_num, count);
  786.  
  787.         if    (error_code != noErr)
  788.         {
  789.             StopAlert(WRITE_ERR,0);
  790.             HUnlock((Handle)w_objs_hndl);
  791.             return(CANCELED);
  792.         }
  793.     
  794.     
  795.  
  796.  
  797. /*************    c)  write the data to the file                **********/
  798.  
  799. /***
  800.     Note:    we are only writing that portion of the WObjs Record 
  801.             that includes the object-count and the objects themselves.
  802.             We have intentionally positioned these items together at 
  803.             the end (bottom) of the definition of the WObjsRecord.  
  804.             
  805.             Thus, by pointing to (**w_objs_hndl).vDocLimit and
  806.             supplying a byte count equal to the size of this portion 
  807.             of the data structure, we can write just the data we want 
  808.             to the file without having to gather it together from 
  809.             separate places into a single block before writing.
  810. ***/
  811.  
  812. /***
  813.     Note:    before writing the data out, copy the print info record from
  814.             the heap into the window objects record.
  815. ***/
  816.  
  817.         //pr_info_hndl = (**w_objs_hndl).prInfoHndl;
  818.         //(**w_objs_hndl).printInfo = **pr_info_hndl;
  819.  
  820.  
  821.         data_ptr = (Ptr)(&(**w_objs_hndl).vDocLimit);
  822.         
  823.         error_code = FSWrite(file_ref_num, &count, data_ptr);
  824.         if    (error_code != noErr)
  825.         {
  826.             StopAlert(WRITE_ERR,0);
  827.             HUnlock((Handle)w_objs_hndl);
  828.             return(CANCELED);
  829.         }
  830.  
  831.  
  832. /*********    d)  close the file                                **********/
  833.  
  834.  
  835.         error_code = FSClose(file_ref_num);
  836.         
  837.         if    (error_code != noErr)
  838.         {
  839.             StopAlert(WRITE_ERR,0);
  840.             HUnlock((Handle)w_objs_hndl);
  841.             return(CANCELED);
  842.         }
  843.  
  844.  
  845. /*************    e)  post the data to the disc                **********/
  846.  
  847.         error_code = FlushVol(0, vol_refnum);
  848.         
  849.         if    (error_code != noErr)
  850.         {
  851.             StopAlert(WRITE_ERR,0);
  852.             HUnlock((Handle)w_objs_hndl);
  853.             return(CANCELED);
  854.         }
  855.  
  856.     }  /*  end of  do_what = SFPUT || do_what == DO_SAVE    */
  857.  
  858.  
  859.  
  860. /***
  861.     we made it!
  862. ***/
  863.  
  864.  
  865.     HUnlock((Handle)w_objs_hndl);
  866.  
  867.     (**w_objs_hndl).dirty = FALSE;
  868.     
  869.     return(SUCCESS);
  870.     
  871. }  /*  end of  do_save_file()    */
  872.  
  873. OSErr get_c_filespec(short wd_refnum,long *dir_id,short *vol_refnum)
  874.  
  875. {
  876.    WDPBRec pb_rec;
  877.    OSErr   err;
  878.  
  879.  
  880.    pb_rec.ioNamePtr  = NIL;
  881.    pb_rec.ioWDProcID = 0L;
  882.    pb_rec.ioWDIndex  = 0;
  883.    pb_rec.ioVRefNum  = wd_refnum;
  884.    
  885.    err = PBGetWDInfo(&pb_rec, FALSE);
  886.    
  887.    if(err == noErr)
  888.    {
  889.         *dir_id     = pb_rec.ioWDDirID;
  890.         *vol_refnum = pb_rec.ioWDVRefNum;
  891.    }
  892.    
  893.    return(err);
  894. }
  895.  
  896.  
  897.  
  898.